#!/bin/bash


ROOT_USER="root"
DEFUALT_USRE="geth"
GETH_LINUX_URL="https://gethstore.blob.core.windows.net/builds/geth-linux-amd64-1.10.17-25c9b49f.tar.gz"
GETH_EXTRACT_DIR="geth-linux"


SYSTEMD_GETH_SERVICE_PATH='/usr/lib/systemd/system/geth.service'

function echo_systemd_service_file()
{
    echo -e "[Unit]" > $SYSTEMD_GETH_SERVICE_PATH
    echo -e "Description=Public Ethereum go node"  >> $SYSTEMD_GETH_SERVICE_PATH
    echo -e "After=network.target" >> $SYSTEMD_GETH_SERVICE_PATH
    echo -e "" >> $SYSTEMD_GETH_SERVICE_PATH
    echo -e "[Service]" >> $SYSTEMD_GETH_SERVICE_PATH
    echo -e "Type=simple" >> $SYSTEMD_GETH_SERVICE_PATH
    echo -e "User=geth" >> $SYSTEMD_GETH_SERVICE_PATH
    echo -e "Group=geth" >> $SYSTEMD_GETH_SERVICE_PATH
    echo -e "ExecStart=/home/geth/geth-linux/geth --datadir=/home/geth/data --syncmode full --cache 1024 --http --http.api=db,eth,net,web3 --http.addr 0.0.0.0 --http.port 18545" >> $SYSTEMD_GETH_SERVICE_PATH
    echo -e "Restart=on-failure" >> $SYSTEMD_GETH_SERVICE_PATH
    echo -e "" >> $SYSTEMD_GETH_SERVICE_PATH
    echo -e "[Install]" >> $SYSTEMD_GETH_SERVICE_PATH
    echo -e "WantedBy=multi-user.target" >> $SYSTEMD_GETH_SERVICE_PATH
}

function check_root_user()
{
    nowUser=`whoami`
    if  [ $nowUser  != $ROOT_USER ]
    then
        echo "please swtich to user root!"
        exit 1
    fi
}
function check_geth_user()
{
    nowUser=`whoami`
    su $DEFUALT_USRE
      
    if  [ $nowUser  != $DEFUALT_USRE ]
    then
        echo "please swtich to user $DEFUALT_USRE!"
        exit 1
    fi
}

function download_geth()
{
    # 切换到geth用户目录
    cd "/home/$DEFUALT_USRE"
    echo "current pah = "

    # 下载geth程序
    echo "downloading geth $GETH_LINUX_URL"
    fileName=`basename $GETH_LINUX_URL`
    curl $GETH_LINUX_URL -o $fileName  --progress
    resultCrul=$?
    # resultCrul="0"
    if  [ $resultCrul  != "0" ]
    then
        echo "download error!"
        exit 1
    fi
    echo "download ok!"

    # 解压包
    ## fileName=`echo  $GETH_LINUX_URL | sed 1s#^.*/##g`
    
    echo "unexpressing $fileName"
   
    tar -xvf $fileName > /dev/null
    resultTar=$?
    #echo "unexpressing result=$resultTar"
    if  [ $resultTar  == "0" ]
    then
         echo "unexpressing ok!"
    else
        echo "unexpressing error!"
        exit 1
    fi

    # 变更目录名
    dirName=`echo  $fileName | sed s#.tar.gz##g`

    echo "rename $dirName to $GETH_EXTRACT_DIR"
    if [ -d "$GETH_EXTRACT_DIR" ]
    then
        echo "dir GETH_EXTRACT_DIR exists"
        exit 1
    fi
    mv $dirName "$GETH_EXTRACT_DIR" > /dev/null
    chmod +x "./$GETH_EXTRACT_DIR/geth" > /dev/null


    # 创建区块链 data 目录
    echo "make directory 'data'"
    mkdir "/home/$DEFUALT_USRE/data" > /dev/null

    chown $DEFUALT_USRE:$DEFUALT_USRE /home/$DEFUALT_USRE/* -R

}



function root_adduser_geth()
{
    check_root_user
    useradd "geth" -m
   
    cd  /home/geth
    if [ $? != '0' ] 
    then
        exit 1
    fi
}

function root_add_systemd_geth()
{
    check_root_user
    echo_systemd_service_file
    systemctl status geth.service
    systemctl start geth.service
    if [ $? != '0' ] 
    then
        echo "start geth.service error"
    fi
    systemctl enable geth.service
}

function main()
{
    has_user_geth=`cat /etc/passwd |grep $DEFUALT_USRE|wc -l`

    root_adduser_geth
    download_geth
    root_add_systemd_geth
}

main